﻿#include "appcore.h"

#include <QDebug>

AppCore* AppCore::m_core = nullptr;

AppCore::AppCore(QObject *parent) : QObject(parent), m_undoGroup(new QUndoGroup(this))
{
    //initUndoAction();
}

AppCore *AppCore::getCore()
{
    if(m_core == nullptr)
        m_core = new AppCore;

    return m_core;
}

void AppCore::releaseCore()
{
    if(m_core != nullptr)
        delete m_core;
    m_core = nullptr;

}

void AppCore::insertAction(const QString &key, QAction *action)
{
    QStringList l = key.split(".");
    l.removeAll("");
    if(l.size() == 0) {
        return;
    }

    QActionArray *group = &m_actions;
    QActionArray *temp;

    foreach(const QString &str, l)
    {
        temp = group->m_childrenMap.value(str);
        if(temp == nullptr) {
            temp = new QActionArray;
            group->m_childrenMap.insert(str, temp);
        }
        group = temp;
    }
    if(group->m_action != nullptr)
    {
        delete group->m_action;
    }
    group->m_action = action;

}

QAction *AppCore::getAction(const QString &key)
{
    QStringList l = key.split(".");
    l.removeAll("");
    if(l.size() == 0) {
        return nullptr;
    }

    QActionArray *group = &m_actions;
    QActionArray *temp;

    foreach(const QString &str, l)
    {
        temp = group->m_childrenMap.value(str);
        if(temp == nullptr) {
            return nullptr;
        }
        group = temp;
    }
    return group->m_action;
}

void AppCore::insertToolBar(const QString &szKey, QToolBar *pToolBarObj)
{
    m_toolBarMap.insert(szKey.toLower(), pToolBarObj);
}

QToolBar *AppCore::getToolBar(const QString &szKey)
{
    return m_toolBarMap.value(szKey.toLower());
}

void AppCore::addUndoStack(QUndoStack *stack)
{
    m_undoGroup->addStack(stack);
}

void AppCore::setActivityStack(QUndoStack *stack)
{
    m_undoGroup->setActiveStack(stack);
}


void AppCore::initUndoAction()
{
}


